<?php

namespace App\Admin\Controllers\Subscribe;

use App\Models\Subscribe\Status;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class StatusListController extends Controller
{
    use ModelForm;

    protected $header = '体验列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Status::class, function (Grid $grid) {

            $grid->model()->getList();

            //$grid->id('记录id')->sortable();
            $grid->user_id('用户id');
            $grid->username('用户名');
            $grid->telephone('手机号');
            $grid->create_at('体验时间')->display(function ($v){
                return date('Y-m-d H:i:s', $v);
            });
            $grid->column('体验天数')->display(function (){
                return number_format(($this->end_time - $this->create_at) / 86400, 2, '.', '');
            });
            $grid->end_time('体验结束时间')->display(function ($v){
                return date('Y-m-d H:i:s', $v);
            });

            $grid->info()->id('是否订阅')->display(function ($id){
                return isset($id) ? '是' : '否';
            });

            $grid->info()->subscribe_time('首次订阅时间');

            //禁用创建按钮
            $grid->disableCreateButton();

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->like('username','用户名称');
                $filter->like('telephone','手机号');
            });

            //操作列
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                //禁用查看
                $actions->disableView();
                //禁用删除
                $actions->disableDelete();
                //禁用修改
                $actions->disableEdit();

                $actions->append('<a href="/users/parentDetail/'.$actions->getKey().'"><i class="fa fa-eye"></i>查看</a>');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Status::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
